using UnityEngine;
using UnityEditor;
using Mirror;

public class QuickFixTools : EditorWindow
{
    // 路径设置
    private string playerPrefabPath = "Prefabs/MinimalPlayer";
    
    [MenuItem("工具/网络设置快速修复")]
    public static void ShowWindow()
    {
        GetWindow<QuickFixTools>("网络设置快速修复");
    }
    
    private void OnGUI()
    {
        GUILayout.Label("Mirror网络设置快速修复工具", EditorStyles.boldLabel);
        EditorGUILayout.Space();
        
        // 显示当前NetworkManager状态
        DisplayNetworkManagerStatus();
        
        EditorGUILayout.Space();
        
        // 路径设置
        playerPrefabPath = EditorGUILayout.TextField("玩家预制体路径", playerPrefabPath);
        
        EditorGUILayout.Space();
        
        // 功能按钮
        if (GUILayout.Button("1. 修复NetworkManager的PlayerPrefab设置"))
        {
            FixPlayerPrefab();
        }
        
        if (GUILayout.Button("2. 重置NetworkManager的PlayerPrefab"))
        {
            ResetPlayerPrefab();
        }
        
        if (GUILayout.Button("3. 验证玩家预制体是否正确"))
        {
            ValidatePlayerPrefab();
        }
        
        EditorGUILayout.Space();
        
        GUILayout.Label("注意: 所有预制体需放在Resources文件夹下才能通过路径加载");
    }
    
    // 显示NetworkManager当前状态
    private void DisplayNetworkManagerStatus()
    {
        NetworkManager networkManager = NetworkManager.singleton;
        if (networkManager != null)
        {
            EditorGUILayout.LabelField("NetworkManager状态:", "已找到");
            
            if (networkManager.playerPrefab != null)
            {
                EditorGUILayout.LabelField("PlayerPrefab:", networkManager.playerPrefab.name);
                
                // 检查NetworkIdentity组件
                NetworkIdentity identity = networkManager.playerPrefab.GetComponent<NetworkIdentity>();
                if (identity != null)
                {
                    EditorGUILayout.LabelField("NetworkIdentity:", "已设置");
                }
                else
                {
                    EditorGUILayout.LabelField("NetworkIdentity:", "未设置 (错误)");
                }
            }
            else
            {
                EditorGUILayout.LabelField("PlayerPrefab:", "未设置 (错误)");
            }
        }
        else
        {
            EditorGUILayout.LabelField("NetworkManager状态:", "未找到 (错误)");
        }
    }
    
    // 修复PlayerPrefab设置
    private void FixPlayerPrefab()
    {
        NetworkManager networkManager = NetworkManager.singleton;
        if (networkManager != null)
        {
            GameObject playerPrefab = Resources.Load<GameObject>(playerPrefabPath);
            if (playerPrefab != null)
            {
                // 检查是否有NetworkIdentity组件
                NetworkIdentity identity = playerPrefab.GetComponent<NetworkIdentity>();
                if (identity == null)
                {
                    Debug.LogError($"预制体 {playerPrefabPath} 缺少NetworkIdentity组件!");
                    EditorUtility.DisplayDialog("错误", $"预制体 {playerPrefabPath} 缺少NetworkIdentity组件!", "确定");
                    return;
                }
                
                // 设置PlayerPrefab
                networkManager.playerPrefab = playerPrefab;
                EditorUtility.SetDirty(networkManager);
                Debug.Log($"已成功设置NetworkManager的PlayerPrefab为: {playerPrefabPath}");
                EditorUtility.DisplayDialog("成功", $"已成功设置NetworkManager的PlayerPrefab为: {playerPrefabPath}", "确定");
            }
            else
            {
                Debug.LogError($"无法加载预制体: {playerPrefabPath}");
                EditorUtility.DisplayDialog("错误", $"无法加载预制体: {playerPrefabPath}\n请确保它位于Resources文件夹下", "确定");
            }
        }
        else
        {
            Debug.LogError("场景中没有NetworkManager实例!");
            EditorUtility.DisplayDialog("错误", "场景中没有NetworkManager实例!", "确定");
        }
    }
    
    // 重置PlayerPrefab
    private void ResetPlayerPrefab()
    {
        NetworkManager networkManager = NetworkManager.singleton;
        if (networkManager != null)
        {
            networkManager.playerPrefab = null;
            EditorUtility.SetDirty(networkManager);
            Debug.Log("已重置NetworkManager的PlayerPrefab设置");
            EditorUtility.DisplayDialog("成功", "已重置NetworkManager的PlayerPrefab设置", "确定");
        }
        else
        {
            Debug.LogError("场景中没有NetworkManager实例!");
            EditorUtility.DisplayDialog("错误", "场景中没有NetworkManager实例!", "确定");
        }
    }
    
    // 验证PlayerPrefab
    private void ValidatePlayerPrefab()
    {
        GameObject playerPrefab = Resources.Load<GameObject>(playerPrefabPath);
        if (playerPrefab != null)
        {
            NetworkIdentity identity = playerPrefab.GetComponent<NetworkIdentity>();
            if (identity != null)
            {
                Debug.Log($"预制体 {playerPrefabPath} 设置正确，包含NetworkIdentity组件");
                EditorUtility.DisplayDialog("验证成功", $"预制体 {playerPrefabPath} 设置正确，包含NetworkIdentity组件", "确定");
            }
            else
            {
                Debug.LogError($"预制体 {playerPrefabPath} 缺少NetworkIdentity组件!");
                EditorUtility.DisplayDialog("验证失败", $"预制体 {playerPrefabPath} 缺少NetworkIdentity组件!", "确定");
            }
        }
        else
        {
            Debug.LogError($"无法加载预制体: {playerPrefabPath}");
            EditorUtility.DisplayDialog("验证失败", $"无法加载预制体: {playerPrefabPath}\n请确保它位于Resources文件夹下", "确定");
        }
    }
} 